#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>


static void cleanup_func(void *p)
{
    puts(p);
} 
//线程处理函数
static void * func(void * p)
{
    puts("thread is working");
    //钩子函数类似物
    pthread_cleanup_push(cleanup_func, "cleanup:1");
    pthread_cleanup_push(cleanup_func, "cleanup:2");
    pthread_cleanup_push(cleanup_func, "cleanup:3");
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);

    puts("push over");

    pthread_exit(NULL);
}

int main()
{
    pthread_t tid;
    int err;

    puts("begin");
    //创建线程
    err = pthread_create(&tid, NULL, func, NULL);
    if(err)
    {
        fprintf(stderr,"pthread_create:%s", strerror(err));
        exit(1);
    }
    //回收线程
    pthread_join(tid, NULL);
    
    puts("end");
    
    exit(0);
}